
import { getExtensionByAppId, getUserTemplateConf } from '@/tools/request/api.js'
import { setToken } from '@/tools/request/request.js'


function judgeTime (time) {
	if (!time) return '';
	let strtime = time.replace(/-/g, '/'); //时间转换
	let date1 = new Date(strtime);
	let date2 = new Date();
	return date1 < date2 ? true : false;
}

export default {
	namespaced: true,
	state: () => ({
		userConfig: uni.getStorageSync('__userConfig') || {},
		template: uni.getStorageSync('templateConfig') || {},
	}),
	mutations: {
		SET_TEMPLATE (state, value) {
			let result = {};
			if (value.templateConf && value.templateConf.templateConfJson) {
				result = JSON.parse(value.templateConf.templateConfJson);
			} else {
				result = JSON.parse(value.templateSettingsJson);
			}
			uni.setStorageSync('templateConfig', result);
			state.template = result;
		},
		SET_ADMIN (state) {
			console.log(content, 'state')
		}
	},
	actions: {
		getExtConfig ({dispatch, commit, rootState}) {
			return new Promise((resolve, reject) => {
				let extConfig = uni.getExtConfigSync() ? uni.getExtConfigSync() : {};
				console.log(extConfig, 'extConfig')
				getExtensionByAppId({appId: extConfig.appid}).then((res) => {
					if (res.code != 200) return uni.$showMsg();
					uni.setStorageSync('__userConfig', res.body);
					// commit('SET_ADMIN')
					rootState.app_store.userServiceToken = res.body.userServiceToken;
					rootState.app_store.adminId = res.body.adminId;
					setToken(res.body.userServiceToken, res.body.adminId);
					resolve(res);
					dispatch('getUserConf', res.body);
				})
			})
		},
		getUserConf({commit, state}, value) {
			let params = {
				userServiceToken: value.userServiceToken
			}
			getUserTemplateConf(params).then(res => {
				if (res.code != 200) return uni.$showMsg();
				commit('SET_TEMPLATE', res.body);
			})
		},
	},
	getters: {
		_temp_auxiliaryColor (state) {
			return state.template.page? state.template.page.auxiliaryColor:''
		},
		_temp_neutralColor (state) {
			return state.template.page? state.template.page.neutralColor:''
		},
		_temp_textColor (state) {
			return state.template.page? state.template.page.textColor:''
		},
		_temp_themeColor (state) {
			return state.template.page? state.template.page.themeColor:''
		},
		_temp_titleColor (state) {
			return state.template.page? state.template.page.titleColor:''
		},
	},
}
